import * as types from '../Commom/ActionConst';
import Global from '../Commom/GlobalConst';
const initialState = {
    isLoading: false,
    isLoggedIn: false,
    isLoggedOut: false,
    isEdit: false,
    isThirdLogin: false,
    phone: '',
    user_name: '',
    head_picture: '',
    kPhoneCode: 0,
    kPhoneForgotPasswordCode: 0,
    kPhoneChangePassWordCode: 0,
};

let UserReducer = (state = initialState, action) => {
    switch (action.type) {
        case types.kUserFromStorage:
            return {
                ...state,
                ...action
            };
        case types.kUserLogin:
            return {
                ...state,
                isLoading: true,
            };
        case types.kUserLoginReceived:
            if (action.result != null) {
                storage.save({
                    key: Global.Const.storageToken,
                    rawData: {
                        token: action.result.token,
                    }
                });
                return Object.assign({}, state, {
                    isLoading: false,
                });
            } else {
                return Object.assign({}, state, {
                    isLoading: false,
                });
            }
        case types.kUserInformation:
            return {
                ...state,
                isLoading: true,
            };
        case types.kUserInformationReceived:
            return {
                ...state,
                ...action
            };
        case types.kUserThirdLogin:
            return {
                ...state,
                isLoading: true,
            };
        case types.kUserThirdLoginReceived:
            return {
                ...state,
                ...action,
                isLoading: false,
            };
        case types.kUserLogout:
            return {
                ...state,
                isLoading: true,
            };
        case types.kUserLogoutReceived:
            return {
                ...state,
                ...action,
                isLoading: false,
            };
        case types.kUserEditData:
            return {
                ...state,
                isEdit: true,
            };
        case types.kUserEditDataReceived:
            return {
                ...state,
                user: action.user,
                isEdit: false,
            };
        case types.kUserInfo:
            return {
                ...state,
                ...action,
            };
        case types.kUserInfoReceived:
            if (action.result != null) {
                storage.save({
                    key: Global.Const.storageUser,
                    rawData: {
                        phone: action.result.phone,
                        user_name: action.result.user_name,
                        head_picture: action.result.head_picture
                    }
                });
                return Object.assign({}, state, {
                    isLoggedIn: true,
                    isLoggedOut: false,
                    isThirdLogin: false,
                    phone: action.result.phone,
                    user_name: action.result.user_name,
                    head_picture: action.result.head_picture

                });
            } else {
                storage.remove({
                    key: Global.Const.storagekeyLogin
                });
                return Object.assign({}, state, {
                    user: [],
                    isLoggedIn: true,
                    isLoggedOut: false,
                    isThirdLogin: false,
                });
            }
        case types.kUserChangePassWord:
            return {
                ...state,
                ...action,
            };
        case types.kUserChangePassWordReceived:
            return {
                ...state,
                ...action,
            };
        case types.kPhoneCodeReceived:
            return Object.assign({}, state, {
                kPhoneCode: 200,
            });
        case types.kPhoneForgotPasswordCodeReceived:
            return Object.assign({}, state, {
                kPhoneForgotPasswordCode: 200,
            });
        case types.kPhoneChangePassWordCodeReceived:
            return Object.assign({}, state, {
                kPhoneChangePassWordCode: 200,
            });
        case types.kPhoneCodeError:
            return Object.assign({}, state, {
                kPhoneCode: -1,
            });
        case types.kPhoneChangePassWordCodeError:
            return Object.assign({}, state, {
                kPhoneChangePassWordCode: -1,
            });
        case types.kPhoneForgotPasswordCodeError:
            return Object.assign({}, state, {
                kPhoneForgotPasswordCode: -1,
            });
        case types.kPhoneCodeClearStatus:
            return Object.assign({}, state, {
                kPhoneCode: 0,
                kPhoneForgotPasswordCode: 0,
                kPhoneChangePassWordCode: 0,
            });
        default:
            return state;
    }
};

export default UserReducer;